package cc.fostion.photoselector.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.isseiaoki.simplecropview.CropImageView;
import com.isseiaoki.simplecropview.callback.CropCallback;

import cc.fostion.photoselector.R;

/**
 * 图片编辑
 * Created by fostion on 5/12/16.
 */
public class EditorActivity extends Activity {

    public static final int FLAG_REQUEST_CROP = 0;

    private ImageView backBtn, cropBtn, rotateBtn;
    private CropImageView cropImageView;
    private TextView doneBtn;
    private boolean isCrop = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_editor);
        setUpView();
        setUpEvent();
    }

    private void setUpView() {
        cropImageView = (CropImageView) findViewById(R.id.cropImageView);
        backBtn = (ImageView) findViewById(R.id.backBtn);
        cropBtn = (ImageView) findViewById(R.id.cropBtn);
        doneBtn = (TextView) findViewById(R.id.doneBtn);
        rotateBtn = (ImageView) findViewById(R.id.rotateBtn);
        cropImageView = (CropImageView) findViewById(R.id.cropImageView);

        if (cropImageView.getImageBitmap() == null) {
            cropImageView.setImageResource(R.drawable.sample1);
        }
        cropImageView.setCropMode(CropImageView.CropMode.CIRCLE_SQUARE);

        cropImageView.setCropEnabled(isCrop);
    }

    private void setUpEvent() {
        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        rotateBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cropImageView.rotateImage(CropImageView.RotateDegrees.ROTATE_90D);
            }
        });

        cropBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isCrop = !isCrop;
                cropImageView.setCropEnabled(isCrop);
            }
        });

        doneBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isCrop) {
                    cropImage();
                } else {
                    //只是获取裁剪图片相当于获取旋转后的图片
                    cropImageView.setCropMode(CropImageView.CropMode.FIT_IMAGE);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            setReturnImage(cropImageView.getCroppedBitmap());
                        }
                    }, 200);
                }
            }
        });

    }

    public void cropImage() {
        cropImageView.startCrop(null, new CropCallback() {
            @Override
            public void onSuccess(Bitmap cropped) {
                setReturnImage(cropped);
            }

            @Override
            public void onError() {
                showToast("裁剪失败");
            }
        }, null);
    }

    @Override
    public void onBackPressed() {
        if (isCrop) {
            isCrop = !isCrop;
            cropImageView.setCropEnabled(isCrop);
            return;
        }
        super.onBackPressed();
    }

    /**
     * 长宽压缩图片
     */
    public void setReturnImage(Bitmap bitmap) {
        if (bitmap == null) {
            finish();
        } else {
            new AsyncTask<Bitmap, Void, Bitmap>() {
                @Override
                protected Bitmap doInBackground(Bitmap... params) {
                    int w = 360;//固定图片宽度
                    int h = 360;//固定图片高度
                    Bitmap newbmp;
                    int width = params[0].getWidth();
                    int height = params[0].getHeight();
                    Matrix matrix = new Matrix();
                    float scaleSize;
                    if (width > height) {
                        scaleSize = ((float) w / width);
                    } else {
                        scaleSize = ((float) h / height);
                    }

                    matrix.postScale(scaleSize, scaleSize);
                    newbmp = Bitmap.createBitmap(params[0], 0, 0, width, height, matrix, true);
                    return newbmp;
                }

                @Override
                protected void onPostExecute(Bitmap bitmap) {
                    returnBitmap(bitmap);
                }
            }.execute(bitmap);
        }
    }

    public void returnBitmap(Bitmap bitmap) {
        Intent result = new Intent();
        result.putExtra("bitmap", bitmap);
        setResult(RESULT_OK, result);
        finish();
    }

    public void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
}

